package com.cqc.jdk8.Jutil.concurrent.locks.ReentrantLock.demo3;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Counter {
    private int count = 0;
    private ReentrantLock lock = new ReentrantLock();

    public void add() {
        lock.lock();
        try {
            System.out.println("add() start..." + Thread.currentThread().getName() + ":" + count);
            TimeUnit.SECONDS.sleep(1);
            count++;
            System.out.println("add() end..." + Thread.currentThread().getName() + ":" + count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void add2() {
        synchronized (this) {
            try {
                System.out.println("add2() start..." + Thread.currentThread().getName() + ":" + count);
                TimeUnit.SECONDS.sleep(1);
                count++;
                System.out.println("add2() end..." + Thread.currentThread().getName() + ":" + count);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public int getCount() {
        return count;
    }
}
